package com.flida.echart.core.order.service.impl;

import com.feilong.core.Validator;
import com.feilong.core.bean.BeanOperationException;
import com.flida.bana.common.util.DateUtils;
import com.flida.bana.common.util.GaodeHelper;
import com.flida.echart.core.api.order.entity.CoreConstant;
import com.flida.echart.core.api.order.entity.OrderTemplate;
import com.flida.echart.core.order.dao.OrderTemplateDao;
import com.flida.echart.core.order.service.IOrderTemplateService;
import org.apache.commons.lang3.Validate;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yzc on 2016/12/11.
 */
@Service("orderTemplateService")
public class OrderTemplateServiceImpl implements IOrderTemplateService {
    private static Logger log4j = Logger.getLogger(OrderServiceImpl.class);
    @Value("${date.formate}")
    public String dateFormate;

    @Autowired(required = true)
    private OrderTemplateDao orderTemplateDao;


    public int deleteByPrimaryKey(String id) {
        return orderTemplateDao.deleteByPrimaryKey(id);
    }


    public int insert(OrderTemplate record) {
        Validate.notNull(record, "record [destination bean] not null!", new Object[0]);
        Validate.notBlank(record.getCityName(), "record.getCityName() [destination bean] not null!", new Object[0]);
        try{
            if (null != record) {
                record.setStatus(CoreConstant.OrderTemplate_status_1);
                record.setDatetime(DateUtils.getNoTime(dateFormate));
            }
            //如果没有经纬度设置，就默认用city的经纬度
            if(Validator.isNullOrEmpty(record.getLatitude())||Validator.isNullOrEmpty(record.getLongitude())){
                double[]  doubles= GaodeHelper.addressToGPS(record.getCityName());
                record.setLatitude(doubles[1]);
                record.setLongitude(doubles[0]);
            }
            return orderTemplateDao.insert(record);
        }catch (Exception ex){
            throw new BeanOperationException(ex);

        }

    }



    public int insertSelective(OrderTemplate record) {

        Validate.notNull(record, "record [destination bean] not null!", new Object[0]);
        Validate.notBlank(record.getCityName(), "record.getCityName() [destination bean] not null!", new Object[0]);
        try{
            if (null != record) {
                record.setStatus(CoreConstant.OrderTemplate_status_1);
                record.setDatetime(DateUtils.getNoTime(dateFormate));
            }
            //如果没有经纬度设置，就默认用city的经纬度
            if(Validator.isNullOrEmpty(record.getLatitude())||Validator.isNullOrEmpty(record.getLongitude())){
                double[]  doubles= GaodeHelper.addressToGPS(record.getCityName());
                record.setLatitude(doubles[1]);
                record.setLongitude(doubles[0]);
            }
            return orderTemplateDao.insertSelective(record);
        }catch (Exception ex){
            throw new BeanOperationException(ex);

        }

    }


    public OrderTemplate selectByPrimaryKey(String id) {
        return orderTemplateDao.selectByPrimaryKey(id);
    }

    public List<OrderTemplate> query(OrderTemplate orderTemplate) {
        return orderTemplateDao.query(orderTemplate);
    }



    public int updateByPrimaryKeySelective(OrderTemplate record) {
        return orderTemplateDao.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(OrderTemplate record) {
        return orderTemplateDao.updateByPrimaryKey(record);
    }
}
